package com.qf.Dao.Impl;/*
 *
 */

import com.qf.Dao.UserDao;
import com.qf.Domain.Address;
import com.qf.Domain.User;
import com.qf.Utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public void insert(User user) {
        String sql="insert into tb_user(username,password,email,gender,flag,role,code) values (?,?,?,?,?,?,?)";
        Object[] params={user.getUsername(),user.getPassword(),user.getEmail(),user.getGender(),user.getFlag(),user.getRole(),user.getCode()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public User select(String username, String password) {
        String sql="select * from tb_user where username=? and password=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql, new BeanHandler<>(User.class), username,password);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public boolean select(String username) {
        //System.out.println(username);
        String sql="select * from tb_user where username=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            User user = qr.query(conn, sql, new BeanHandler<>(User.class), username);
            //System.out.println(user);
            if (user!=null){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public List<Address> selectAddress(Integer uid) {
        String sql="select * from tb_address where uid=?";
        Connection conn= DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql,new BeanListHandler<>(Address.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void addAddress(Address address) {
        String sql="insert into tb_address(detail,name,phone,uid,level) values (?,?,?,?,?)";
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel()};
        Connection conn= DataSourceUtils.getConnection();
        try {
            qr.update(conn,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void updateAddressStatus(Integer uid, Integer aid) {
        String sql1="update tb_address set level=0 where uid=?";
        String sql2="update tb_address set level=1 where id=?";
        Connection conn = DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql1,uid);
            qr.update(conn, sql2,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void updateAddress(Address address) {
        String sql="update tb_address set detail=?,name=?,phone=?,level=? where id=?";
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getLevel(),address.getId()};
        Connection conn = DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void delete(int aid) {
        String sql="delete from tb_address where id=?";
        Connection conn = DataSourceUtils.getConnection();
        try {
            qr.update(conn, sql,aid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }
}
